package Cn.edu.zafu.bigdata.x.cxy;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
import java.sql.*;

public class StudentInfoManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("程序退出。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }




    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try (Connection conn = DatabaseConnection.getConnection()) {
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM student202203090119 WHERE id = ?");
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("学号重复，请重新输入。");
                return;
            }

            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO student202203090119 (id, name, phone) VALUES (?, ?, ?)");
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, phone);
            int rowsInserted = pstmt.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("学生信息添加成功。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void searchStudent(Scanner scanner) {
        System.out.print("请输入学号进行检索: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try (Connection conn = DatabaseConnection.getConnection()) {
                PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM student202203090119 WHERE id = ?");
                pstmt.setString(1, id);
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    Student student = new Student(rs.getString("id"), rs.getString("name"), rs.getString("phone"));
                    System.out.println(student.toString());
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.print("请输入学号进行检索（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    private static void updateStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try (Connection conn = DatabaseConnection.getConnection()) {
                PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM student202203090119 WHERE id = ?");
                pstmt.setString(1, id);
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    PreparedStatement updateStmt = conn.prepareStatement("UPDATE student202203090119 SET name = ?, phone = ? WHERE id = ?");
                    updateStmt.setString(1, name);
                    updateStmt.setString(2, phone);
                    updateStmt.setString(3, id);
                    int rowsUpdated = updateStmt.executeUpdate();
                    if (rowsUpdated > 0) {
                        System.out.println("学生信息更新成功。");
                    }
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.print("请输入学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    private static void deleteStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try (Connection conn = DatabaseConnection.getConnection()) {
                PreparedStatement pstmt = conn.prepareStatement("DELETE FROM student202203090119 WHERE id = ?");
                pstmt.setString(1, id);
                int rowsDeleted = pstmt.executeUpdate();
                if (rowsDeleted > 0) {
                    System.out.println("学生信息删除成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.print("请输入学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }
}

class DatabaseConnection {
    private static final String URL = "jdbc:mysql://192.168.169.76:3306/learnjava";
    private static final String USER = "learn";
    private static final String PASSWORD = "learnpassword";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
}

class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}